package com.example.allshengmedical.base;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Locale;

import com.example.allshengmedical.experimentanalyze.bean.CtSettingBean;
import com.example.allshengmedical.util.LoadingDialog;
import com.example.allshengmedical.util.SharedPreferencesUtils;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by WillJoe on 16/8/24.
 */
public abstract class BaseActivity extends FragmentActivity {

	LoadingDialog progressDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		// 保证每次都是橫屏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		super.onCreate(savedInstanceState);
		// 每打开一个activity就会往baseapplication的list中放入此activity
		BaseApplication.getInstance().addActivity(this);

		if ("zh".equals(SharedPreferencesUtils.get(this, BaseApplication.SYSTEM_LANGUAGE, ""))) {
			Locale.setDefault(Locale.ENGLISH);
			Configuration config = getBaseContext().getResources().getConfiguration();
			config.locale = Locale.ENGLISH;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());

		} else if ("en".equals(SharedPreferencesUtils.get(this, BaseApplication.SYSTEM_LANGUAGE, ""))) {
			Locale.setDefault(Locale.CHINA);
			Configuration config = getBaseContext().getResources().getConfiguration();
			config.locale = Locale.CHINA;
			getBaseContext().getResources().updateConfiguration(config,
					getBaseContext().getResources().getDisplayMetrics());

		} else if ("".equals(SharedPreferencesUtils.get(this, BaseApplication.SYSTEM_LANGUAGE, ""))) {

		}
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(setcontent());
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
		progressDialog = new LoadingDialog();
		progressDialog.setMenuVisibility(true);
		smartInject();// 自动获取控件
		initView();
		initData();

		// 切换横竖屏
		showOrientation();

	}

	private void showOrientation() {
		if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// 当前是横屏

		}

		else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// 当前是竖屏

		}

	}

	// 绑定布局
	protected abstract int setcontent();

	// 绑定布局
	protected abstract void initView();

	// 数据的处理
	protected abstract void initData();

	public void showProgressDialog(String message, boolean flag) {
		if (progressDialog == null)
			return;
		setProgressDialogMessage(message);
		progressDialog.show(getFragmentManager(), message);
		progressDialog.setCancelable(flag);

	}

	public void setProgressDialogMessage(String message) {
		progressDialog.setMsg(message);
	}

	public void closeProgressDialog() {

		if (progressDialog != null) {
			if (progressDialog.getDialog().isShowing()) {
				progressDialog.dismiss();
			}
		}

	}

	// 方便绑定布局的
	protected <T extends View> T BindView(int id) {
		return (T) findViewById(id);
	}

	private void smartInject() {

		try {
			Class<? extends Activity> clz = getClass();

			while (clz != BaseActivity.class) {

				Field[] fs = clz.getDeclaredFields();
				Resources res = getResources();
				String packageName = getPackageName();
				System.out.println("packageName" + packageName);
				for (Field field : fs) {

					System.out.println("field.getType()" + field.getType());
					if (!View.class.isAssignableFrom(field.getType())) {
						continue;
					}
					System.out.println("field.getName()" + field.getName());

					int viewId = res.getIdentifier(field.getName(), "id", packageName);
					System.out.println("viewId" + viewId);
					if (viewId == 0)
						continue;
					field.setAccessible(true);
					try {
						View v = findViewById(viewId);
						field.set(this, v);
						Class<?> c = field.getType();
						Method m = c.getMethod("setOnClickListener", android.view.View.OnClickListener.class);
						m.invoke(v, this);
					} catch (Throwable e) {
					}
					field.setAccessible(false);

				}

				clz = (Class<? extends Activity>) clz.getSuperclass();

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	protected void onDestroy() {
		BaseApplication.getInstance().removeActivity(this);
		super.onDestroy();
	}

	public void SendMessageValue(ArrayList<CtSettingBean> ctSettingBeans, Boolean defaultBaselineBoo) {
		// TODO Auto-generated method stub

	}

}
